﻿using System;
using System.Data;
using System.Data.SqlClient;

namespace Module.QuanTriHeThong.DanhMuc
{
    public class DMNhaCungCap : DBInteractionBase
    {
        public int ID { get; set; }
        public string TenNhaCungCap { get; set; }
        public string MaSoThue { get; set; }
        public string DiaChi { get; set; }
        public string SoDienThoai { get; set; }
        public bool IsActive { get; set; }

        public bool Insert(DMNhaCungCap obj)
        {
            try
            {
                using (var dbcmd = new SqlCommand("tbDuoc_DMNhaCungCap_Insert", GetConnection()))
                {
                    dbcmd.CommandType = CommandType.StoredProcedure;
                    dbcmd.Parameters.Add(new SqlParameter("@TenNhaCungCap", obj.TenNhaCungCap));
                    dbcmd.Parameters.Add(new SqlParameter("@MaSoThue", obj.MaSoThue));
                    dbcmd.Parameters.Add(new SqlParameter("@DiaChi", obj.DiaChi));
                    dbcmd.Parameters.Add(new SqlParameter("@SoDienThoai", obj.SoDienThoai));
                    dbcmd.ExecuteNonQuery();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool Update(DMNhaCungCap obj)
        {
            try
            {
                using (var dbcmd = new SqlCommand("tbDuoc_DMNhaCungCap_Update", GetConnection()))
                {
                    dbcmd.CommandType = CommandType.StoredProcedure;
                    dbcmd.Parameters.Add(new SqlParameter("@ID", obj.ID));
                    dbcmd.Parameters.Add(new SqlParameter("@TenNhaCungCap", obj.TenNhaCungCap));
                    dbcmd.Parameters.Add(new SqlParameter("@MaSoThue", obj.MaSoThue));
                    dbcmd.Parameters.Add(new SqlParameter("@DiaChi", obj.DiaChi));
                    dbcmd.Parameters.Add(new SqlParameter("@SoDienThoai", obj.SoDienThoai));
                    dbcmd.ExecuteNonQuery();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool Delete(int id)
        {
            try
            {
                using (var dbcmd = new SqlCommand("tbDuoc_DMNhaCungCap_Delete", GetConnection()))
                {
                    dbcmd.CommandType = CommandType.StoredProcedure;
                    dbcmd.Parameters.Add(new SqlParameter("@ID", id));
                    dbcmd.ExecuteNonQuery();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public DataTable SelectById(int id)
        {
            var dt = new DataSet();
            try
            {
                using (var dbcmd = new SqlCommand("tbDuoc_DMNhaCungCap_SelectById", GetConnection()))
                {
                    dbcmd.CommandType = CommandType.StoredProcedure;
                    dbcmd.Parameters.Add(new SqlParameter("@ID", id));
                    var dtAdapter = new SqlDataAdapter(dbcmd);
                    dtAdapter.Fill(dt);
                    return dt.Tables[0];
                }
            }
            catch (Exception ex)
            {
                throw new Exception("tbDuoc_DMNhaCungCap_SelectById: " + ex.Message);
            }
        }

        public DataTable SelectAll(DMNhaCungCap obj, int pageindex, int pagesize, out int recordcount)
        {
            var dt = new DataSet();
            try
            {
                using (var dbcmd = new SqlCommand("tbDuoc_DMNhaCungCap_SelectAll", GetConnection()))
                {
                    dbcmd.CommandType = CommandType.StoredProcedure;
                    dbcmd.Parameters.Add(new SqlParameter("@TenNhaCungCap", string.IsNullOrEmpty(obj.TenNhaCungCap) ? "" : obj.TenNhaCungCap));
                    dbcmd.Parameters.Add(new SqlParameter("@MaSoThue", string.IsNullOrEmpty(obj.MaSoThue) ? "" : obj.MaSoThue));
                    dbcmd.Parameters.Add(new SqlParameter("@DiaChi", string.IsNullOrEmpty(obj.DiaChi) ? "" : obj.DiaChi));
                    dbcmd.Parameters.Add(new SqlParameter("@SoDienThoai", string.IsNullOrEmpty(obj.SoDienThoai) ? "" : obj.SoDienThoai));
                    dbcmd.Parameters.Add(new SqlParameter("@PageIndex", pageindex));
                    dbcmd.Parameters.Add(new SqlParameter("@PageSize", pagesize));
                    dbcmd.Parameters.Add(new SqlParameter("@RecordCount", SqlDbType.Int, 50) { Direction = ParameterDirection.Output });
                    var dtAdapter = new SqlDataAdapter(dbcmd);
                    dtAdapter.Fill(dt);
                    recordcount = Convert.ToInt32(dbcmd.Parameters["@RecordCount"].Value);
                    return dt.Tables[0];
                }
            }
            catch (Exception ex)
            {
                throw new Exception("tbDuoc_DMNhaCungCap_SelectAll: " + ex.Message);
            }
        }
    }
}
